package com.cdm.forum.service.impl;

import com.cdm.forum.common.Result;
import com.cdm.forum.common.ResultCode;
import com.cdm.forum.dao.ArticleMapper;
import com.cdm.forum.dao.ArticleReplyMapper;
import com.cdm.forum.exception.ApplicationException;
import com.cdm.forum.model.ArticleReply;
import com.cdm.forum.service.IArticleReplyService;
import com.cdm.forum.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private IArticleService articleService;

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void createArticleReply(ArticleReply articleReply) {
        //校验参数
        if(articleReply == null || articleReply.getArticleId() == null
                || articleReply.getPostUserId() == null
                || StringUtils.isEmpty(articleReply.getContent() == null)) { //回复帖子的id，回复网友的id，内容都不为空
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));

        }

        //设置默认值
        //楼中楼
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        //回复点赞数
        articleReply.setLikeCount(0);
        //支持该回复的回复，0 允许 1 禁止
        articleReply.setState((byte) 0);
        //没有删除该评论
        articleReply.setDeleteState((byte) 0);

        //创建时间，更新时间
        Date now = new Date();
        articleReply.setCreateTime(now);
        articleReply.setUpdateTime(now);

        //写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //更新帖子表的回复数
        articleService.addOneArticleReplyCountById(articleReply.getArticleId());

    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //校验参数
        if(articleId == null || articleId <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用mapper
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);
        if(articleReplies == null || articleReplies.size() <= 0) {
            articleReplies = new ArrayList<ArticleReply>();
        }

        //返回
        return articleReplies;
    }
}
